import { Navigate, useLocation } from "react-router-dom"
import { isAuth } from "@/utils/token"

export default function WithAuth({ children }) {
  const { pathname } = useLocation()
  if (isAuth()) {
    // 已登录
    if (pathname === "/login") {
      return <Navigate to="/" replace></Navigate>
    } else {
      return <>{children}</>
    }
  } else {
    // 未登录
    if (pathname === "/login") {
      return <>{children}</>
    } else {
      return <Navigate to="/login" replace></Navigate>
    }
  }
}
